function formatTime(time) {
	if (typeof time !== 'number' || time < 0) {
		return time
	}
	var day = parseInt(time / (24 * 3600))
	time = time % (24 * 3600)
	var hour = parseInt(time / 3600)
	time = time % 3600
	var minute = parseInt(time / 60)
	time = time % 60
	var second = time

	return ([day, hour, minute, second]).map(function (n) {
		n = n.toString()
		return n[1] ? n : '0' + n
	}).join(':')
}

function formatLocation(longitude, latitude) {
	if (typeof longitude === 'string' && typeof latitude === 'string') {
		longitude = parseFloat(longitude)
		latitude = parseFloat(latitude)
	}

	longitude = longitude.toFixed(2)
	latitude = latitude.toFixed(2)

	return {
		longitude: longitude.toString().split('.'),
		latitude: latitude.toString().split('.')
	}
}
var dateUtils = {
	UNITS: {
		'年': 31557600000,
		'月': 2629800000,
		'天': 86400000,
		'小时': 3600000,
		'分钟': 60000,
		'秒': 1000
	},
	humanize: function (milliseconds) {
		var humanize = '';
		for (var key in this.UNITS) {
			if (milliseconds >= this.UNITS[key]) {
				humanize = Math.floor(milliseconds / this.UNITS[key]) + key + '前';
				break;
			}
		}
		return humanize || '刚刚';
	},
	format: function (dateStr) {
		var date = this.parse(dateStr)
		var diff = Date.now() - date.getTime();
		if (diff < this.UNITS['天']) {
			return this.humanize(diff);
		}
		var _format = function (number) {
			return (number < 10 ? ('0' + number) : number);
		};
		return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDay()) + '-' +
			_format(date.getHours()) + ':' + _format(date.getMinutes());
	},
	parse: function (str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串，转化为一个Date对象
		var a = str.split(/[^0-9]/);
		return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
	}
};

//格式：2017年10月11 12时12分
function g_zhcnDate(shijianchuo, str) {
	//shijianchuo是整数，否则要parseInt转换  
	var time = new Date(shijianchuo);
	var y = time.getFullYear();
	var m = time.getMonth() + 1;
	var d = time.getDate();
	var h = time.getHours();
	var mm = time.getMinutes();
	var s = time.getSeconds();
	if (str) {
		return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s)
	}
	return y + '-' + add0(m) + '-' + add0(d); // + '-' //+ add0(h) + ':' + add0(mm);  //+':'+add0(s)
	//return y + '年' + add0(m) + '月' + add0(d) + '日 ' //+ add0(h) + ':' + add0(mm);  //+':'+add0(s)
};
//格式：2017年10月11 12时12分
function g_md(shijianchuo, str) {
	//shijianchuo是整数，否则要parseInt转换  
	var time = new Date(shijianchuo);
	var y = time.getFullYear();
	var m = time.getMonth() + 1;
	var d = time.getDate();
	var h = time.getHours();
	var mm = time.getMinutes();
	var s = time.getSeconds();
	if (str) {
		return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s)
	}
	return y + '-' + add0(m) + '-' + add0(d); // + '-' //+ add0(h) + ':' + add0(mm);  //+':'+add0(s)
	//return y + '年' + add0(m) + '月' + add0(d) + '日 ' //+ add0(h) + ':' + add0(mm);  //+':'+add0(s)
};
function add0(m) {
	return m < 10 ? '0' + m : m
};
//去除字符串 去除空格  去除特殊字符 去除空行
function get_html(sa) {
	if (!sa) {
		return "";
	}
	return sa.replace(/<[^>]+>/g, "").replace(/\ +/g, "").replace(/[\r\n]/g, "");

}
function get_urlid(url, name) {
	var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i");
	if (reg.test(url)) return unescape(RegExp.$2.replace(/\+/g, " "));
	return "";
}
function share(type, scene) {
	uni.share({
		provider: "weixin",
		scene: scene ? "WXSenceTimeline" : "WXSceneSession",
		type: type,
		href: "http://app.yiyul.cn/yiyule_download/",
		title: "立富科技",
		summary: "立富科技平台",
		imageUrl: "../../../static/img/logo180-180.png",
		success: function (res) {
			console.log("success:" + JSON.stringify(res));
		},
		fail: function (err) {
			console.log("fail:" + JSON.stringify(err));
		}
	});
}
function scanCode(callback) {
	uni.scanCode({
		onlyFromCamera: true,// 只允许通过相机扫码
		success: function (res) {
			console.log('条码类型：' + res.scanType);
			console.log('条码内容：' + res.result);
			callback(res)
		}
	});
}
function apptoast(str) {
	//#ifndef APP-PLUS
	uni.showToast({
		title: str,
		icon: 'none',
		position: 'bottom',
		duration: 1000
	});
	return;
	//#endif
	// #ifdef APP-PLUS
	plus.nativeUI.toast(str);
	// #endif
}
function isPhone(phone) {
	return /^1([3456789])[0-9]{9}$/.test(phone);
};
var uapi = {
	request_url: 'https://app.yiyul.cn/api/v1/',
}
function _get(url, method, data, load, hide) {
	if (load != 'none') {
		uni.showLoading({
			title: load ? load : '加载中',
			mask: true,
		});
	}
	var obj = data;
	var token = uni.getStorageSync('token');

	if (url.indexOf('?') > '-1') {

		url = url + '&token=' + token

	} else {

		url = url + '?token=' + token
	}

	console.log('提交的参数：' + JSON.stringify(data))
	return new Promise((resove, reject) => {
		uni.request({
			url: uapi.request_url + url,
			data: obj || {},
			header: { 'content-type': 'application/x-www-form-urlencoded' },
			method: method || 'GET',
			success: (res) => {
				if (hide != 'hide') {
					try {
						uni.hideLoading();
					} catch (e) {
						//TODO handle the exception
					}
				}
				if (res.data.code == 200) {
					resove(res.data);
				} else {
					reject(res.data);
					uni.showToast({
						title: res.data.msg,
						image: '../../../static/img/logo180-180.png',
						position: 'center',
						duration: 1000
					});
				}
				// console.log(res.data);
			},
			fail: function (res) {
				try {
					uni.hideLoading();
				} catch (e) {
					//TODO handle the exception
				}
				reject(res);
				uni.showToast({
					title: '请求失败',
					image: '../../../static/img/logo180-180.png',
					position: 'center',
					duration: 1000
				});
			},
			complete: function (res) {

			},
		});
	})
}
function _upload(url, filePath, data, load) {
	if (load != 'none') {
		uni.showLoading({
			title: load ? load : '加载中',
			mask: true,
		});
	}
	var obj = data;
	var token = uni.getStorageSync('token');
	if (url.indexOf('?') > '-1') {
		url = url + '&token=' + token
	} else {
		url = url + '?token=' + token
	}

	// console.log('提交的参数：'+JSON.stringify(data))
	return new Promise((resove, reject) => {
		uni.uploadFile({
			url: uapi.request_url + url,
			filePath: filePath,
			name: 'img',
			formData: obj,
			success: (res) => {
				var data = JSON.parse(res.data);
				try {
					uni.hideLoading();
				} catch (e) {
					//TODO handle the exception
				}
				if (data.code == 200) {
					resove(data);
				} else {
					reject(data);
					uni.showToast({
						title: data.msg,
						image: '../../../static/img/logo180-180.png',
						position: 'center',
						duration: 1000
					});
				}
				// console.log(res.data);
			},
			fail: function (res) {
				try {
					uni.hideLoading();
				} catch (e) {
					//TODO handle the exception
				}
				reject(res);
				uni.showToast({
					title: '请求失败',
					image: '../../../static/img/logo180-180.png',
					position: 'center',
					duration: 1000
				});
			},
			complete: function (res) {

			},
		});
	})
}

function _goto(url) {
	uni.navigateTo({
		url: url
	});
}

function getCurrentDate() {
	var timeStr = '-';
	var curDate = new Date();
	var curYear = curDate.getFullYear();  //获取完整的年份(4位,1970-????)
	var curMonth = curDate.getMonth() + 1;  //获取当前月份(0-11,0代表1月)
	var curDay = curDate.getDate();       //获取当前日(1-31)
	var curWeekDay = curDate.getDay();    //获取当前星期X(0-6,0代表星期天)
	var curHour = curDate.getHours();      //获取当前小时数(0-23)
	var curMinute = curDate.getMinutes();   // 获取当前分钟数(0-59)
	var curSec = curDate.getSeconds();      //获取当前秒数(0-59)
	var Current = curYear + timeStr + curMonth + timeStr + curDay + ' ' + curHour + ':' + curMinute + ':' + curSec;
	console.log(Current);
	// this.datetime=Current;
	return Current;
}
module.exports = {
	getCurrentDate:getCurrentDate,
	formatTime: formatTime,
	formatLocation: formatLocation,
	dateUtils: dateUtils,
	_get: _get,
	_goto: _goto,
	isPhone: isPhone,
	apptoast: apptoast,
	_upload: _upload,
	get_html: get_html,
	share: share,
	scanCode: scanCode,
	get_urlid: get_urlid,
	g_zhcnDate: g_zhcnDate
}
